<template>
    <div class="page-container">
        <div class="echarts-container" ref="main"></div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            myEcharts: null
        }
    },
    computed:{
        themeStatus(){
            return this.$store.state.theme
        }
    },
    watch:{
        themeStatus(){
            this.myEcharts.dispose()
            this.initEcharts()
            this.getData()
            this.screen()
        }
    },
    mounted() {
        this.initEcharts()
        this.getData()
        window.addEventListener('resize',this.screen)
        this.screen()
    },
    destroyed(){
        window.removeEventListener('reset',this.screen)
    },
    methods: {
        // 适配屏幕
        screen(){
            this.myEcharts.resize()
            // 计算一个文字大小的比例
            let width = this.$refs.main.offsetWidth
            let num = width / 100 * 6
            let options = {
                title:{
                    textStyle:{  // 设置主标题样式
                        fontSize:num
                    }
                }
            }
            // 修改图标样式以后要重绘
            this.myEcharts.setOption(options)
        },
        // 初始化图标
        initEcharts() {
            let main = this.$refs.main
            // 初始化echarts
            this.myEcharts = this.$echarts.init(main,this.themeStatus)            /** @type EChartsOption */
            let options = {  // 设置图标的属性和样式
                title: {  // 主标题
                    text: '各地区种植面积统计TOP10',
                    left: 5,
                    top: 5
                },
                grid: {
                    left: '15%',
                    right: '10%',
                    top: '13%',
                    bottom: '10%',
                },
                tooltip: {
                    show: true,
                },
                xAxis: {
                    show: true,
                    type: 'value',
                    axisLine: {
                        show: true,
                    },
                },
                toolbox: {  // 工具箱
                    show: true,
                    feature: {
                        saveAsImage: {  // 保存图片
                            type: 'png',
                        },
                        dataView: {
                            show: true,
                        },
                    },
                },
                axisPointer: {
                    show: true
                },
                yAxis: {
                    show: true,
                    type: 'category',
                    // data: nameData
                },
                series: [
                    {
                        type: 'bar',
                        // data: areaData,
                        label: {  // 定义显示数据
                            show: true,
                            position: 'right',
                        },
                        itemStyle: {  // 设置图标颜色
                            opacity: 0.8,  // 设置柱状的透明度
                            borderRadius: [0,15,15,0],
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x1: 1,
                                y1: 0,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: '#0F3357'
                                    },
                                    {
                                        offset: 1,
                                        color: '#36BFE5'
                                    }
                                ]
                            }
                        },
                    }
                ],
            }
            // 绘制图标
            this.myEcharts.setOption(options)
        },
        // 获取数据
        async getData() {
            let dataList = await this.$http('/api/plantingAreaTop')
            dataList.data.sort((a, b) => {
                return a.area - b.area
            })
            let nameData = dataList.data.map(item => {
                return item.name
            })
            let areaData = dataList.data.map(item => {
                return item.area
            })
            this.setEcharts(nameData,areaData)
        },
        // 绘制图标
        setEcharts(nameData,areaData) {
            let options = {
                yAxis:{
                    data:nameData
                },
                series:[
                    {
                        data:areaData
                    }
                ]
            }
            this.myEcharts.setOption(options)
        }
    }
}
</script>

<style></style>